﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using facebook.Schema;
using facebook.Utility;

namespace facebook
{
    public class connect
    {
        private readonly API _api;
        public connect(API parent)
		{
			_api = parent;
		}

        ///<summary>
        /// Lets Facebook know that these emails are no longer members of your site.
        ///</summary>
        ///<param name="_email_hashes">an array of strings from registerUsers</param>
        ///<returns></returns>
        public bool unregisterUsers(List<string> _email_hashes)
        {
            var _session_key = _api.SessionKey;
            _api.SessionKey = null;

            // Unregister the account from fb
            var _result = false;
            
            var parameterList = new Dictionary<string, string> { { "method", "facebook.connect.unregisterUsers" } };
            _api.AddOptionalParameter(parameterList, "email_hashes", JSONHelper.ConvertToJSONArray(_email_hashes));
            var response = _api.SendRequest(parameterList);
            _result = string.IsNullOrEmpty(response) ||  connect_unregisterUsers_response.Parse(response).connect_unregisterUsers_response_elt.Count == _email_hashes.Count;

            _api.SessionKey = _session_key;
            return _result;
        }
        
        ///<summary>
        /// Register new accounts with Facebook to facilitate friend linking.
        /// Note: this is an optional step, and only makes sense if you have
        /// a site with an existing userbase that you want to tie into
        /// Facebook Connect.
        ///</summary>
        ///<param name="_accounts">array of accounts, each with keys 'email_hash' and 'account_id'</param>
        ///<returns>whether the emails were registered. true unless there's an error</returns>
        public bool registerUsers(List<Dictionary<string,string>> _accounts) 
        {
            var _session_key = _api.SessionKey;
            _api.SessionKey = null;

            bool _result = false;
            
            var parameterList = new Dictionary<string, string> { { "method", "facebook.connect.registerUsers" } };
            var itemList = new List<string>();
            foreach (var account in _accounts)
            {
                itemList.Add(JSONHelper.ConvertToJSONAssociativeArray(account));
            }
            _api.AddOptionalParameter(parameterList, "accounts", JSONHelper.ConvertToJSONArray(itemList));
            var response = _api.SendRequest(parameterList);
            
            // On success, return the set of email hashes registered
            // An email hash will be registered even if the email does not match a Facebook account
            _result = string.IsNullOrEmpty(response) || 
                connect_registerUsers_response.Parse(response).connect_registerUsers_response_elt.Count == _accounts.Count;

            _api.SessionKey = _session_key;
            return _result;
        }

        /// <summary>
        /// This method returns the number of friends of the current user who have accounts on your site, but have not yet connected their accounts
        /// </summary>
        /// <returns>indicates the number of users who have not yet connected their accounts</returns>
        public int getUnconnectedFriends()
        {
            var parameterList = new Dictionary<string, string> { { "method", "facebook.connect.getUnconnectedFriendsCount" } };
            var response = _api.SendRequest(parameterList);
            return !string.IsNullOrEmpty(response) ? connect_getUnconnectedFriendsCount_response.Parse(response).TypedValue : 0;
        }
    }
}
